package com.bird.system.remote.auth

import cn.dev33.satoken.annotation.SaIgnore
import com.bird.component.cache.component.CacheComponent
import com.bird.component.lang.common.response.JSONResponse
import com.bird.component.lang.common.response.success
import com.bird.component.lang.util.log
import com.bird.component.log.WebLog
import com.bird.component.sensitive.toJacksonJsonStr
import com.bird.system.application.auth.AuthCommandService
import org.springframework.web.bind.annotation.*
import com.bird.system.application.auth.pl.*

@SaIgnore
@RestController
@RequestMapping("/auth")
class LoginController(
    private val authCommandService: AuthCommandService,
    private val cacheComponent: CacheComponent
) {

    /**
     * 获取验证码
     */
    @WebLog(description = "获取验证码")
    @GetMapping("/captcha")
    fun verificationCode(): JSONResponse<VerificationCodeResponse> {
        return success {
            // 生成验证码
            val (key, imageBase64) = authCommandService.generateVerificationCode()
            // 返回验证码
            VerificationCodeResponse(key, imageBase64)
        }
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    fun login(@RequestBody loginRequest: LoginReqeust): JSONResponse<LoginResponse> {
        return success {
            authCommandService.login(loginRequest)
        }
    }

    @GetMapping("/test")
    fun testCache(){
        cacheComponent.setValue(
            "test", listOf(
                LoginReqeust("test", "test", "test", "test"),
                LoginReqeust("test2", "test2", "test2", "test2")
            )
        )
        log.info(cacheComponent.getValue<List<LoginReqeust>>("test").toJacksonJsonStr())
    }
}